<?php
class Tools_Response
{
   public static function Error($status = 1, $msg = 'fail')
    {
        $callback = ParamPackTool::getCallback();
        $res = json_encode(array("status" => $status, "msg" => $msg), JSON_UNESCAPED_UNICODE);
        if ("" != $callback) {
            $res = 'try{var jsondata=' . $res . ';' . $callback . '(jsondata);}catch(e){jsonError(e)}';
        }
        LogTool::responseerrorLog()->log($res);
        return $res;
    }
    
    public static function ErrorEcho($status = 1, $msg = 'fail', $data = []) {
        $callback = ParamPackTool::getCallback();
        $ret = [];
        $ret['status'] = $status;
        $ret['msg']     = $msg;
        if(!empty($data)){
            $ret['data']     = $data;
        }
        $ret['s']     = time();
        $res = json_encode($ret, JSON_UNESCAPED_UNICODE);
        if ("" != $callback) {
            $res = 'try{var jsondata=' . $res . ';' . $callback . '(jsondata);}catch(e){jsonError(e)}';
        }
        LogTool::responseerrorLog()->log($res);
        return $res;
    }

    public static function Succ($res = array(), $need = false, $cachekey = '')
    {
        $callback = ParamPackTool::getCallback();
        $response = array('status' => 0, 'msg' => 'ok');
        if ($res) $response['data'] = $res;
        if (!$res && $need) $response['data'] = (object) array();
        $res = json_encode($response, JSON_UNESCAPED_UNICODE);
        if ("" != $callback) {
            $res = 'try{var jsondata=' . $res . ';' . $callback . '(jsondata);}catch(e){jsonError(e)}';
        }
        LogTool::responsesuccLog()->log($res);
        if ($cachekey) self::ByRedis($cachekey, $res);
        return $res;
    }

    public static function ByRedis($key, $data)
    {
        $extime = 60;
        $_redisTool = RedisTool::getInstance();
        $_redisTool->set($key, $data, $extime);
    }
    /**
     * ajax请求响应内容
     * @param int $code
     * @param string $msg
     * @param string $content
     */
    public static function ajaxResponse($content = '', $code = 1, $msg = 'success'){
        echo json_encode([
                'code' => $code,
                'msg'  => $msg,
                'content' => $content,
            ]);
        return false;
    }
}
